package org.example.project01.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.project01.common.PageData;
import org.example.project01.common.ResponseData;
import org.example.project01.domain.GoodsList;
import org.example.project01.mapper.GoodsListMapper;
import org.example.project01.service.IGoodsListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsListImpl implements IGoodsListService {
@Autowired
private GoodsListMapper goodsListMapper;
    @Override
    public ResponseData<GoodsList> addGoods(GoodsList goodsList) {
        if (goodsListMapper.addGoods(goodsList)==1)
            return ResponseData.ok();
        else
            return ResponseData.fail();
    }

    @Override
    public ResponseData<GoodsList> deleteGoods(String goodsId) {
        if (goodsListMapper.deleteGoods(goodsId)==1)
            return ResponseData.delete();
        else
            return ResponseData.fail();
    }

    @Override
    public ResponseData<GoodsList> modifyGoods(GoodsList goodsList) {
        if (goodsListMapper.modifyGoods(goodsList)==1)
            return ResponseData.modify();
        else
            return ResponseData.fail();
    }

    @Override
    public ResponseData<PageData <GoodsList>> getGoods(String goodsId) {
        PageHelper.startPage(1,10);
        PageInfo<GoodsList>pageInfo=new PageInfo<>(goodsListMapper.getGoods(goodsId));
        PageData<GoodsList>pageData=new PageData<>(pageInfo.getTotal(),pageInfo.getList());
        if (ObjectUtil.isNotNull(goodsListMapper.getGoods(goodsId)))
            return ResponseData.ok(pageData);
        else
            return ResponseData.fail();
    }
}
